package com.example.task4.util;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * 数据库访问工具类
 */
public class DataBaseUtil {
    static Properties p;
    static String driverClassName,url,userName,pwd;
    //加载驱动
    static{
        p=new Properties();
        try {
            p.load(DataBaseUtil.class.getResourceAsStream("/db.properties"));
            driverClassName=p.getProperty("driverClassName");
            url=p.getProperty("url");
            userName=p.getProperty("username");
            pwd=p.getProperty("password");
            Class.forName(driverClassName);
        } catch (IOException e) {
            System.out.println(e.getMessage());
        } catch (ClassNotFoundException e) {
            System.out.println(e.getMessage());
        }
    }
    public static Connection getConnection(){
        try {
            return  DriverManager.getConnection(url,userName,pwd);
        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
        System.out.println("链接为空！");
        return null;
    }
    public static void close(Connection con){
        try {
            con.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
